﻿using Models.Common;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;

namespace Web.Common
{
    public class Bill1
    {
        public static Result<string> IndexEdit(Models.MVC.Bill1IndexModel m)
        {
            Result<string> result = new Result<string>();
            if (m.TouQty<=0)
            {
                result.Type = AjaxResultType.Error;
                result.Content = "投片数量必须大于0。";
                result.Data = "投片数量必须大于0。";
                return result;
            }
            try
            {
                if (m.ID == 0)
                {
                    if (Convert.ToInt32(FreeADO.DBUtility.DbHelperSQL.GetSingle("select count(*) from BillIndex1 where Code='" + m.Code + "' and IsDeleted=0")) > 0)
                    {
                        result.Type = AjaxResultType.Error;
                        result.Content = "卡号已经存。";
                        result.Data = "卡号已经存。";
                        return result;
                    }

                    FreeADO.DBUtility.DbHelperSQL.ExecuteSql(@"
INSERT INTO [dbo].[BillIndex1]
           ([Code]
           ,[GoodsID]
           ,[SourceGoodsID]
           ,[DianZhu]
           ,[LingDate]
           ,[LingUserID]
           ,[TouDate]
           ,[TouUserID]
           ,[Number]
           ,[TouQty]
           ,[HuoYuanHao]
           ,[JianYanHao]
           ,[ZhengBei]
           ,[DengJi]
           ,[WaiXing]
           ,[State]
           ,[BigClassID]
           ,[XinPianMianJi]
           ,[IsDeleted])
     VALUES
           (
'" + m.Code + @"',
'" + m.GoodsID + @"',
'" + m.SourceGoodsID + @"',
'" + m.DianZhu + @"',
'" + m.LingDate + @"',
'" + m.LingUserID + @"',
'" + m.TouDate + @"',
'" + m.TouUserID + @"',
'" + m.Number + @"',
'" + m.TouQty + @"',
'" + m.HuoYuanHao + @"',
'" + m.JianYanHao + @"',
'" + m.ZhengBei + @"',
'" + m.DengJi + @"',
'" + m.WaiXing + @"',
'0',
'" + m.BigClassID + @"',
'" + m.XinPianMianJi + @"',
0
)
");
                    if (m.MoBanID>0)
                    {
                        int IndexID = Convert.ToInt32(FreeADO.DBUtility.DbHelperSQL.GetSingle("select ID from BillIndex1 where Code='" + m.Code + "' and IsDeleted=0"));
                        FreeADO.DBUtility.DbHelperSQL.ExecuteSql(@"
insert into BillProcedures1(BillIndexID,ProcedureID,Remark,ShowIndex,BiaoZhunQty)
select " + IndexID + @",ProcedureID,GongYi,ShowIndex,BiaoZhunQty
from BillTempletProcedures
where IndexID=" + m.MoBanID + @" and IsDeleted=0
order by ShowIndex

");
                    }
                    result.Type = AjaxResultType.Success;
                    result.Content = "成功";
                    result.Data = "成功";
                }
                else
                {
                    if (Convert.ToInt32(FreeADO.DBUtility.DbHelperSQL.GetSingle("select count(*) from BillIndex1 where Code='" + m.Code + "' and IsDeleted=0 and ID<>" + m.ID)) > 0)
                    {
                        result.Type = AjaxResultType.Error;
                        result.Content = "卡号已经存。";
                        result.Data = "卡号已经存。";
                        return result;
                    }
                    FreeADO.DBUtility.DbHelperSQL.ExecuteSql(@"
UPDATE [dbo].[BillIndex1]
   SET [Code] = '" + m.Code + @"'
      ,[GoodsID] = '" + m.GoodsID + @"'
      ,[SourceGoodsID] = '" + m.SourceGoodsID + @"'
      ,[TouQty] = '" + m.TouQty + @"'
      ,[Number] = '" + m.Number + @"'
      ,[DianZhu] = '" + m.DianZhu + @"'
      ,[LingDate] = '" + m.LingDate + @"'
      ,[LingUserID] = '" + m.LingUserID + @"'
      ,[TouDate] = '" + m.TouDate + @"'
      ,[TouUserID] = '" + m.TouUserID + @"'
      ,[HuoYuanHao] = '" + m.HuoYuanHao + @"'
      ,[JianYanHao] = '" + m.JianYanHao + @"'
      ,[ZhengBei] = '" + m.ZhengBei + @"'
      ,[DengJi] = '" + m.DengJi + @"'
      ,[WaiXing] = '" + m.WaiXing + @"'
      ,[BigClassID] = '" + m.BigClassID + @"'
      ,[XinPianMianJi] = '" + m.XinPianMianJi + @"'
 WHERE ID=" + m.ID + @"
");
                    result.Type = AjaxResultType.Success;
                    result.Content = "成功";
                    result.Data = "成功";
                }
            }
            catch (Exception e)
            {

                result.Type = AjaxResultType.Error;
                result.Content = e.Message;
                result.Data = e.Message;
            }
            return result;
        }

        public static Result<int> BillDetailsEdit(Models.MVC.Bill1DetailsModel m)
        {
            Result<int> result = new Result<int>();
            try
            {
                string err = FreeADO.DBUtility.DbHelperSQL.GetSingle("select dbo.Fn_Value_CheckProQty(1," + m.BillProcedureID + "," + m.ID + "," + m.InQty + "," + m.FanQty + "," + m.FailQty + "," + m.OutQty + ")").ToString();
                if (err != "")
                {
                    result.Type = AjaxResultType.Error;
                    result.Content = err;
                    result.Data = 0;
                }
                else
                {
                    if (m.ID == 0)
                    {

                        FreeADO.DBUtility.DbHelperSQL.ExecuteSql(@"
INSERT INTO [dbo].[BillDetails1]
           ([BillProcedureID]
           ,[GongYiID]
           ,[InQty]
           ,[OutQty]
           ,[FanQty]
           ,[FailQty]
           ,[FailRemark]
           ,[TaskTime]
           ,[GongYi]
           ,[OperatorUserID]
           ,[CreateDate]
           ,[WenDu]
           ,[ShiDu]
           ,[BeginDate]
           ,[EndDate]
           ,[Remark])
     VALUES
          (
            '" + m.BillProcedureID + @"',
            '" + m.GongYiID + @"',
            '" + m.InQty + @"',
            '" + m.OutQty + @"',
            '" + m.FanQty + @"',
            '" + m.FailQty + @"',
            '" + m.FailRemark + @"',
            '" + m.TaskTime + @"',
            @GongYi,
            '" + m.OperatorUserID + @"',
            '" + m.CreateDate + @"',
            '" + m.WenDu + @"',
            '" + m.ShiDu + @"',
            '" + m.BeginDate + @"',
            '" + m.EndDate + @"',
            '" + m.Remark + @"'
            )
", new SqlParameter[1] { new SqlParameter("@GongYi", String.IsNullOrEmpty(m.GongYi) ? "" : m.GongYi) });
                    }
                    else
                    {
                        FreeADO.DBUtility.DbHelperSQL.ExecuteSql(@"
UPDATE [dbo].[BillDetails1]
   SET [BillProcedureID] = '" + m.BillProcedureID + @"'
      ,[GongYiID] = '" + m.GongYiID + @"'
      ,[InQty] = '" + m.InQty + @"'
      ,[OutQty] = '" + m.OutQty + @"'
      ,[FanQty] = '" + m.FanQty + @"'
      ,[FailQty] = '" + m.FailQty + @"'
      ,[FailRemark] = '" + m.FailRemark + @"'
      ,[TaskTime] = '" + m.TaskTime + @"'
      ,[GongYi] = @GongYi
      ,[OperatorUserID] = '" + m.OperatorUserID + @"'
      ,[CreateDate] = '" + m.CreateDate + @"'
      ,[WenDu] = '" + m.WenDu + @"'
      ,[ShiDu] = '" + m.ShiDu + @"'
      ,[Remark] = '" + m.Remark + @"'
      ,[BeginDate] = '" + m.BeginDate + @"'
      ,[EndDate] = '" + m.EndDate + @"'
 WHERE ID=" + m.ID, new SqlParameter[1] { new SqlParameter("@GongYi", String.IsNullOrEmpty(m.GongYi) ? "" : m.GongYi) });
                    }
                    result.Type = AjaxResultType.Success;
                    result.Content = "成功。";
                    result.Data = 0;
                }
            }
            catch (Exception e)
            {

                result.Type = AjaxResultType.Error;
                result.Content = e.Message;
                result.Data = 0;
            }
            return result;
        }

    }
}